var config = require('./../config.js');
var domain = config.host;
const app = getApp();

// var domain = 'http://127.0.0.1:8088/api';

var funcs = {
    loginBool: false,
    loginuserinfo:false,
    domain: domain,
    setApiToken: function() {
        if (this.loginBool == false) {
            this.loginBool = true;
            var self = this;
            // wx.getUserInfo({
            //     success: function(res) {
                    wx.login({
                        success: function(data) {
                            wx.request({
                                url: config.openIdUrl,
                                data: {
                                    code: data.code,
                                    appid: config.appid,
                                    // userinfo: res.rawData,
                                    user_id: app.globalData.options
                                },
                                success: function(res) {
                                    console.log('拉取openid成功', res)
                                    try {
                                        wx.setStorageSync('api_token', res.data);
                                        wx.setStorageSync('apiToken', res.data);
                                    } catch (e) {}
                                },
                                fail: function(res) {
                                    console.log('拉取用户openid失败，将无法正常使用开放接口等服务', res)
                                }
                            })
                        },
                        fail: function(err) {
                            console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
                        }
                    })

            //     }
            // })
        }

    },
    login: function() {
        this.setApiToken();
    },
    getApiToken: function() {
        var self = this;
        try {
            var value = wx.getStorageSync('apiToken')
            var value2 = wx.getStorageSync('api_token')
            if (value && value2) {
                return value;
            } else {
                this.login();
                return 'lccydefaultapitoken';
            }
        } catch (e) {}
    },
    getHeader: function() {
        var api_token = this.getApiToken();
        if (api_token) {
            return {
                'content-type': 'application/json',
                'Authorization': 'Bearer ' + api_token,
                // 'Authorization': 'Bearer JZuw7KoEyCgRpxkYb9WtUzqVRIZOUz6IrwcMPvOBdU2TDjImpsLdrebUkY1V',
                'Accept': 'application/json'
            }
        } else {
            return false;
        }
    },
    showLoading: function(e = '加载中') {
        wx.showLoading({
            title: e,
            mask: true
        })
    },
    showSuccess: function(e = '成功') {
        wx.showToast({
          title: e,
          icon: 'success',
          duration: 2000
        })
    },
    showwarn: function(e = '失败') {
        wx.showToast({
          title: e,
          image : '/static/images/cancel.png',
          duration: 1000,
          mask: true
        })
    },
    checkwarn: function(e) {
        if(e.code != 200) {
            this.showwarn(e.msg);
            return false;
        } else {
            this.hideLoading();
            return true;
        }
    },
    resultShowModal: function(res) {
        wx.showModal({
            title: '错误',
            content: res.msg,
            showCancel: false,
        });
    },
    hideLoading: function() {
        wx.hideLoading()
    },
    getusermoney: function(callback) {
        var self = this;
        this.showLoading();
        if (this.getHeader()) {
            return wx.request({
                // url: domain + '/wechat/al/item/' + id,
                url: domain + '/wechat/al/apiUsermoney',
                method: 'post',
                header: this.getHeader(),
                success: function(res) {
                    self.hideLoading();
                    callback(res.data);
                },
                fail: function(res) {
                    self.hideLoading();
                    console.log(res);
                }
            })
        }
    },
 
    createOrder: function(data, callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiOrder',
            method: 'post',
            data: data,
            header: this.getHeader(),
            success: function(res) {
                // self.hideLoading();
                if (res.data.code == 200) {
                    self.getPaySign(res.data, callback);
                } else {
                    self.hideLoading();
                    self.resultShowModal(res.data);
                }
            },
            fail: function (res) {
                self.hideLoading();
                console.log(res);
            }
        })
    },
    getPaySign: function(res, callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/pay',
            method: 'post',
            data: {
                no: res.data.no ? res.data.no : res.data[res.data.name],
                model: res.data.model ? res.data.model : '',
                name: res.data.name ? res.data.name : '',
            },
            header: this.getHeader(),
            success: function(res) {
                self.pay(res.data, callback);
            }
        })
    },
    pay: function(res, callback) {
        var self = this;
        wx.requestPayment({
            'timeStamp': res.timeStamp,
            'nonceStr': res.nonceStr,
            'package': res.package,
            'signType': res.signType,
            'paySign': res.paySign,
            success: function(e) {
                self.hideLoading();
                callback(e)
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },

    getshoplist: function(callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiShop',
            method: 'get',
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getshopinfo: function(id, callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiShop/' + id,
            method: 'get',
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getmyorders: function(callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiMyorders',
            method: 'get',
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getuserbalance: function(callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiUserbalance',
            method: 'post',
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getuseralipay: function(callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiGetuseralipay',
            method: 'post',
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getalipay: function(data,callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiUseralipay',
            method: 'post',
            data: data,
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getmoney: function(data,callback) {
        var self = this;

        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiGetmoney',
            method: 'post',
            data: data,
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getuserlow: function(callback) {
        var self = this;

        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiUserlower',
            method: 'post',
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getcode: function(callback) {
        var self = this;
        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiCode',
            method: 'post',
            header: this.getHeader(),
            data: {
              appid: 'wx1574a600da8780fa'
            },
            success: function(res) {
                self.hideLoading();
                console.log(res);
                callback(res.data);
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    },
    getrun: function (callback) {
      var self = this;
      this.showLoading();
      if (this.getHeader()) {
        return wx.request({
          // url: domain + '/wechat/shop/item/' + id,
          url: domain + '/wechat/al/apiIndex',
          method: 'get',
          data:{
            appid: 'wx1574a600da8780fa'
          },
          header: this.getHeader(),
          success: function (res) {
            self.hideLoading();
            callback(res.data);
          },
          fail: function (res) {
            self.hideLoading();
            console.log(res);
          }
        })
      }
    },
    upuserinfo: function(data, callback) {
        var self = this;

        self.showLoading();
        wx.request({
            url: domain + '/wechat/al/apiUserinfo',
            method: 'post',
            data: data,
            header: this.getHeader(),
            success: function(res) {
                self.hideLoading();
                if(self.checkwarn(res.data)) {
                    callback(res.data);
                }
            },
            fail: function(res) {
                self.hideLoading();
            }
        })
    }

}

module.exports = funcs;
